package com.cn.yang.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.news.NewsModel;
import com.cn.yang.model.production.ProductModel;
import com.cn.yang.service.NewsService;
import com.cn.yang.service.ProductService;
import com.cn.yang.service.impl.NewsServiceImpl;
import com.cn.yang.service.impl.ProductServiceImpl;
import com.cn.yang.util.UpLoad;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/news/updateNews")
public class UpdateNewsServlet extends HttpServlet {
    //查询数据，返回==============
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer id = Integer.valueOf(req.getParameter("id"));
        NewsService service = new NewsServiceImpl();
        NewsModel model = service.selectById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("newsModel",model);
        resp.getWriter().println(jsonObject);
    }
    // 更新数据库===========================================================
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        Integer classify = Integer.valueOf(req.getParameter("newsClassify"));
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String time = req.getParameter("newsTime");
        NewsModel model = new NewsModel();
        model.setId(id);
        model.setTitle(title);
        model.setContent(content);
        model.setTime(time);
        model.setType(classify);

        NewsService service = new NewsServiceImpl();
        int result = service.updateNews(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);

    }
}
